package com.itzb.asone.admin.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itzb.asone.application.BaseController;
import com.itzb.asone.constant.SystemConstant;
import com.itzb.asone.constant.model.SysGroupResourceConstant;
import com.itzb.asone.mybatis.Criteria;
import com.itzb.asone.system.model.SysGroupResource;
import com.itzb.asone.system.model.SysResource;
import com.itzb.asone.system.service.IMessageService;
import com.itzb.asone.system.service.ISysDictService;
import com.itzb.asone.system.service.ISysGroupResourceService;
import com.itzb.asone.system.service.ISysOrgService;
import com.itzb.asone.system.service.ISysResourceService;
import com.itzb.asone.utils.GitId;
import com.itzb.asone.utils.JsonResult;
import com.itzb.asone.utils.PageResult;

@Controller
@RequestMapping(value = "power")
public class PowerController extends BaseController {
	
	private Logger logger = Logger.getLogger(getClass()); 
	
	@Autowired
	private IMessageService messageService;
	@Autowired
	private ISysOrgService sysOrgService;
	@Autowired
	private ISysDictService sysDictService;
	@Autowired
	private ISysResourceService sysResourceService;
	@Autowired
	private ISysGroupResourceService sysGroupResourceService;

	/**
	 * 显示权限列表
	 * @param map
	 * @param request
	 * @param response
     * @return
     */
	@RequestMapping(value = "/list", method = { RequestMethod.GET })
	public String list(ModelMap map, HttpServletRequest request,HttpServletResponse response) {
		return "system/power/list";
	}

	/**
	 * 获取权限列表
	 * @param request
	 * @param response
     * @return
     */
	@ResponseBody
	@RequestMapping(value = "/getdata", method = { RequestMethod.POST })
	public PageResult getData(HttpServletRequest request,HttpServletResponse response) {
		String groupId = request.getParameter("groupId");
		PageResult pageResult = new PageResult();	
		if(groupId == null || groupId.equals("")){
			return pageResult;
		}
		Criteria<SysGroupResource> paramSysGroupResource = new Criteria<SysGroupResource>();
		paramSysGroupResource.addParam("groupId", groupId);
		List<SysGroupResource> listSysGroupResource = sysGroupResourceService.queryPage(paramSysGroupResource);
		Criteria<SysResource> param = new Criteria<SysResource>();
		List<SysResource> list = sysResourceService.queryPage(param);
		List<SysResource> root = new ArrayList<SysResource>();
		List<SysResource> result = new ArrayList<SysResource>();
		for (SysResource sysResource : list) {
			if(sysResource.getParentResourceId().equals(SystemConstant.TOP_MENU_CODE)){
				root.add(sysResource);
			}
		}
		for (SysResource rootSysResource : root) {
			rootSysResource.setResourceName(rootSysResource.getResourceName());
			result.add(rootSysResource);
			result = getSysResource(list, rootSysResource, result,0);
		}
		
		for (SysResource sysResource : result) {
			for(SysGroupResource sysGroupResource : listSysGroupResource){
				if(sysResource.getResourceId().equals(sysGroupResource.getResourceId())){
					sysResource.setPrower("true");
				}
			}
		}
		
		pageResult.setTotal(sysResourceService.queryPageCount(param));
		pageResult.setRows(sysDictService.translateToMapList(result));
		return pageResult;
	}
	
	private List<SysResource> getSysResource(List<SysResource> list,SysResource rootSysResource,List<SysResource> result,int num){
		num+=1;
		for (SysResource sysResource : list) {
			if(rootSysResource.getResourceId().equals(sysResource.getParentResourceId())){
				sysResource.setResourceName(getLine(num) + sysResource.getResourceName());
				result.add(sysResource);
				if(isExistChild(list, sysResource)){
					result = getSysResource(list, sysResource, result,num);
				}
			}
		}
		return result;
	}

	private boolean isExistChild(List<SysResource> list,SysResource sysResource){
		for (SysResource temp : list) {
			if(temp.getParentResourceId().equals(sysResource.getResourceId())){
				return true;
			}
		}
		return false;
	}
	
	private String getLine(int num){
		String i = "";
		for (int j = 0; j < num; j++) {
			i += "————";
		}
		return i + "&nbsp;&nbsp;";
	}
	
	/**
	 * 新增权限
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/modify", method = { RequestMethod.POST })
	public JsonResult modify(SysGroupResource sysGroupResource,HttpServletRequest request,HttpServletResponse response) {
		JsonResult jsonResult = JsonResult.newInstance();
		String power = request.getParameter("power");
		Criteria<SysGroupResource> param = new Criteria<SysGroupResource>();
		param.addParam("groupId", sysGroupResource.getGroupId());
		param.addParam("resourceId", sysGroupResource.getResourceId());
		List<SysGroupResource> list = sysGroupResourceService.queryPage(param);

		if(power.equals("true")){
			if(list.size() > 0){
				jsonResult.declareSuccess();
				return jsonResult;
			} else {
				sysGroupResource.setId(GitId.primaryKey());
				sysGroupResource.setAccessAuth(Integer.parseInt(SysGroupResourceConstant.T_SYS_GROUP_RESOURCE_STATUS_2));
				sysGroupResource.setCreateTime(new Date());
				sysGroupResourceService.insert(sysGroupResource);
			}
		} else {
			sysGroupResourceService.deleteByCriteria(param);
		}

		
		jsonResult.declareSuccess();
		return jsonResult;
	}
	

}
